package com.j180.controller.comment;

import com.github.pagehelper.PageInfo;
import com.j180.bean.Comment;
import com.j180.bean.ResultBean;
import com.j180.bean.Store;
import com.j180.service.comment.ICommentService;
import com.j180.service.store.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    //注入
    @Autowired
    private ICommentService iCommentService;

    /**
     * 查询显示所有评论信息
     * @return 查询结果集
     */
    @GetMapping("/showAllComment")
    public ResultBean <List<Comment>> showAllComment(){
        ResultBean <List<Comment>> allComment = new ResultBean<>();
        
        List<Comment>  comments = iCommentService.showAllComment();
        
        if(comments.size()==0){
            allComment.setCode(0);
            allComment.setMsg("没查到符合条件的数据");
            allComment.setT(null);
        }else{
            allComment.setCode(1);
            allComment.setMsg("查到"+comments.size()+"条,符合条件的数据");
            allComment.setT(comments);
        }
        
        return allComment;
    }
        
    /**
     * 根据评论ID查询评论详细信息
     * @param id 评论id
     * @return 查询结果
     */
    @GetMapping("/showDetailComment/{id}")
    public ResultBean<Comment> showDetailCommentById(@PathVariable("id") int id){
        ResultBean<Comment> CommentDetailForId = new ResultBean<>();
        
       Comment comment = iCommentService.showDetailCommentById(id);
       
       if(comment==null){
         CommentDetailForId.setCode(0);
         CommentDetailForId.setMsg("没有查到符合条件的信息");
         CommentDetailForId.setT(null);
       }else{
           CommentDetailForId.setCode(1);
           CommentDetailForId.setMsg("找到符合条件的信息");
           CommentDetailForId.setT(comment);
       }
        return CommentDetailForId;
    }

    /**
     * 添加一条评论
     * @param comment 评论实体类
     * @return 成功  "add success" 失败 "add failure"
     */
    @PostMapping("/addComment")
    public ResultBean<String> addComment(Comment comment){
        ResultBean<String> addComment = new ResultBean<>();
        comment.setTime(LocalDateTime.now());
        
        int num = iCommentService.addComment(comment);

        if(num==0){
            addComment.setCode(0);
            addComment.setMsg("添加失败");
            addComment.setT("failure");
        }else{
            addComment.setCode(1);
            addComment.setMsg("添加成功");
            addComment.setT("success");
        }
        return addComment;
    }

    /**
     * 根据评论ID删除一条评论
     * @param id 评论ID
     * @return 成功  "del success" 失败 "del failure"
     */
    @DeleteMapping("/deleteComment/{id}")
    public ResultBean<String> delComment(@PathVariable("id")int id){
        ResultBean<String> delComment = new ResultBean<>();
        
        int num = iCommentService.delComment(id);

        if(num==0){
           delComment.setCode(0);
           delComment.setMsg("删除失败");
            delComment.setT("failure");
        }else{
            delComment.setCode(1);
            delComment.setMsg("删除成功");
            delComment.setT("success");
        }
        return  delComment;
    }

    /**
     * 修改评论
     * @param comment 评论实体类
     * @return 成功  "edit success" 失败 "edit failure"
     */
    @PutMapping("/editComment")
    public ResultBean<String> editComment(Comment comment){
        ResultBean<String> editComment =new ResultBean<>();
        comment.setTime(LocalDateTime.now());
        int num = iCommentService.editComment(comment);

        if(num==0){
           editComment.setCode(0);
           editComment.setMsg("修改失败");
           editComment.setT("failure");
        }else{
            editComment.setCode(1);
            editComment.setMsg("修改成功");
            editComment.setT("success");
        }
        return editComment;
    }

    /**
     * 评论查询分页显示
     * @param curPage 分页页数
     * @param pageSize 每页显示条数
     * @return 查询结果
     */
    @GetMapping("/pagingComment/{curPage}/{pageSize}")
    public ResultBean <List<Comment>> pageInfo(@PathVariable("curPage") String curPage, @PathVariable("pageSize") String pageSize){
        ResultBean <List<Comment>> Forpaging = new ResultBean<>();
        
        List<Comment> pages = iCommentService.pageInfo(Integer.parseInt(curPage),Integer.parseInt(pageSize)).getList();

        if(pages.size()==0){
            Forpaging.setCode(0);
            Forpaging.setMsg("没有符合条件的信息");
            Forpaging.setT(null);
        }else{
            Forpaging.setCode(1);
            Forpaging.setMsg("找到"+pages.size()+"条,符合条件的信息");
            Forpaging.setT(pages);
        }
        return Forpaging;
    }
}
